package JDBC_learn;

import org.junit.Test;

import java.sql.*;

public class JDBCDemo7 {
   @Test
     public void JDBCDemo_userlogin() throws SQLException {
       String url = "jdbc:mysql:///itheima?useSSL=false";
       String username = "root";
       String password = "11235813";
       Connection conn = DriverManager.getConnection(url, username, password);
     //接收用户输入，用户名和密码
       String name="魏正想";
       String pwd="11235813";
       String sql="select*from tb_user where name=?and password=?";
       //获取pstmt对象
     PreparedStatement pstmt = conn.prepareStatement(sql);
     //设置？的值
     pstmt.setString(1,name);
     pstmt.setString(2,pwd);
     ResultSet rs=pstmt.executeQuery();
       if(rs.next()){
           System.out.println("登陆成功");
       }else {
           System.out.println("登陆失败");
       }
         rs.close();
         pstmt.close();
         conn.close();

     }
     //PreparedStatement原理
  @Test
  public void JDBCDemo_userlogin1() throws SQLException {
    String url = "jdbc:mysql:///itheima?useSSL=false&useServerPrepStmts=true";
    String username = "root";
    String password = "11235813";
    Connection conn = DriverManager.getConnection(url, username, password);
    //接收用户输入，用户名和密码
    String name="魏正想";
    String pwd="11235813";
    String sql="select*from tb_user where name=?and password=?";
    //获取pstmt对象
    PreparedStatement pstmt = conn.prepareStatement(sql);
    //设置？的值
    pstmt.setString(1,name);
    pstmt.setString(2,pwd);
    ResultSet rs=pstmt.executeQuery();
    if(rs.next()){
      System.out.println("登陆成功");
    }else {
      System.out.println("登陆失败");
    }
    rs.close();
    pstmt.close();
    conn.close();

  }
}
